<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.4);
            /* margin: 100px; */
            /* transform: translate(); */
            /* position: absolute; */
            position: absolute;
            left: 100px;
            top: 100px;
        }

        .sub {
            width: 100px;
            height: 100px;
            background-color: #6ff;
            margin: 50px;
            pointer-events: none;
        }
    </style>
</head>
<body>
    <div class="shadow">
        <!-- <div class="sub"></div> -->
    </div>
</body>
<script>
    var shadow = document.getElementsByClassName("shadow")[0];

    var dragEle = {
        target: shadow,
        dargStart:function(){
            var _this = this;// this 指向dargEle
            this.target.onmousedown = function(e){
                var e = e || window.event;

                var posX = e.offsetX;
                var posY = e.offsetY;
                // console.log(posX,posY);
                // console.log(this);//  this指向target

                _this.posX = posX;
                _this.posY = posY;
                // console.log(_this);// _this 指向dargEle

                _this.dargMove();
                _this.dargEnd();

            }
        },
        dargMove:function(){
            document.onmousemove = (e) => {
                var e = e || window.event;
                var x = e.pageX - this.posX;
                var y = e.pageY - this.posY;
                // console.log(this);//this 指向dargEle

                this.target.style.left = x + "px";
                this.target.style.top = y + "px";
            }
        },
        dargEnd: function(){
            document.onmouseup = function(){
                document.onmousemove = null;
            }
        }
    }
    dragEle.dargStart();
</script>
</html>